import datetime

from fastapi import APIRouter, Body, Query, Depends

from common.pkg.fastapi_util import cbv
from common.route.mall_route import MallMatchAPIRoute
from common.route.route import ZipCustomerRoute
from common.route.tenant_route import ClientTenantBaseAPIRoute
from common.route.user_route import ClientMustLoginAPIRoute
from common.utils.depends.common import get_page_info, Page
from core.view import ClientBaseView
from enums.order_enum import ClientQueryOrderStatusEnum
from schema.req_schema.order_schema import AddNewOrderModel
from service.order_sercice import OrderService

router = APIRouter(prefix="/order", route_class=ZipCustomerRoute(ClientTenantBaseAPIRoute,
                                                                 MallMatchAPIRoute,
                                                                 ClientMustLoginAPIRoute))


@cbv(router)
class OrderView(ClientBaseView):
    @router.post("", description="下单")
    async def create_order(self,
                           order_model: AddNewOrderModel = Body(...),
                           ):
        dt = datetime.datetime.now().date()
        os = OrderService(self.mgw, self.redis)
        order_model.precheck = False
        ok, data_or_code = await os.create_order(self.mall_id, self.user_id, order_model, dt)
        return self.unpack_response(ok, data_or_code)

    @router.post("/precheck", description="下单预检")
    async def order_precheck(self,
                             order_model: AddNewOrderModel = Body(...),
                             ):
        dt = datetime.datetime.now().date()
        os = OrderService(self.mgw, self.redis)
        order_model.precheck = True
        ok, data_or_code = await os.create_order(self.mall_id, self.user_id, order_model, dt)
        return self.unpack_response(ok, data_or_code)

    @router.get("/{order_sn:str}", description="订单详情")
    async def get_order_detail(self, order_sn: str):
        ...

    @router.get("", description="订单列表")
    async def get_order_list(self,
                             status: ClientQueryOrderStatusEnum = Query(ClientQueryOrderStatusEnum.ALL, description="状态, 待付款-1 待发货-2 待收货-3 待评价-4 全部-all/None"),
                             page_info: Page = Depends(get_page_info)
                             ):
        # 待付款 order_status = 1 pay_status = 1
        # 待发货 order_status = 1 pay_status = 2 shipping_status = 2 complaint_status = 1
        # 待收货 order_status = 1 pay_status = 2 shipping_status = 3 complaint_status = 1
        # 待评价 order_status = 1 pay_status = 2 shipping_status = 4 complaint_status = 1
        oss = OrderService(self.mgw, self.redis)
        ok, data_or_code = await oss.get_order_list(self.mall_id, page_info, status, self.user_id)
        return self.unpack_response(ok, data_or_code)

    @router.put("/{order_sn:str}", description="取消订单")
    async def cancel_order(self, order_sn: str):
        oss = OrderService(self.mgw, self.redis)
        ok, data_or_code = await oss.cancel_order(self.mall_id, order_sn, self.user_id)
        return self.unpack_response(ok, data_or_code)

